package com.bycx.dingxinlc.model;

import java.io.Serializable;
import java.util.Date;

import com.bycx.wechat.util.WeChatSessionUtil;

/** 短信验证码
 * 
 * @ClassName: SmsValicode
 * @author Allen
 * @date 2018年4月12日 */
public class SmsValicode implements Serializable {
	private static final long serialVersionUID = 3921143627001241570L;

	private String phoneNo;// 手机号
	private String code;// 验证码
	private Date sendTime;// 发送时间
	private Integer validSec;// 有效时长（秒）

	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Date getSendTime() {
		return sendTime;
	}

	public void setSendTime(Date sendTime) {
		this.sendTime = sendTime;
	}

	public Integer getValidSec() {
		return validSec;
	}

	public void setValidSec(Integer validSec) {
		this.validSec = validSec;
	}

	public Date getOverdueTime() {
		if (this.sendTime == null || this.validSec == null)
			return null;
		return new Date(this.sendTime.getTime() + this.validSec.longValue() * 1000);
	}

	/** 校验验证码 */
	public boolean validate(String phoneNo,
			String code,
			Date now) {
		if (!this.phoneNo.equals(phoneNo))
			return false;

		if (!this.code.equals(code))
			return false;

		Date overdueTime = this.getOverdueTime();
		if (overdueTime.compareTo(now) < 0)
			return false;

		WeChatSessionUtil.delSmsValicode();

		return true;
	}

	public boolean validate(String phoneNo,
			String code) {
		return validate(phoneNo, code, new Date());
	}

}
